<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\IcRecord;
use App\Models\FlowExtemal;
use App\Exports\FlowExtemalExport;
use Maatwebsite\Excel\Facades\Excel;

class FlowExtemalController extends Controller
{
    public function getFlowExtemal(Request $request){

    	$icRecords = IcRecord::with(['flowExtemal'])->get();

    	foreach ($icRecords as $icRecord) {
    		if($icRecord->flowExtemal == null && $icRecord->order_systemnumber != null){
    			FlowExtemal::create([
    				'order_systemnumber' => $icRecord->order_systemnumber
    			]);
    		}
    	}

        $icRecords = IcRecord::query();

        if(isset($request->start_time)){
            if(isset($request->end_time)){
                $icRecords = $icRecords->whereHas('weightdata',function($query) use ($request){
                    $query->where('second_time','>=',$request->start_time)->where('second_time','<=',$request->end_time);
                });
            }
        }


        if(isset($request->process_number)){
            $icRecords = $icRecords->whereHas('weightdata',function($query) use ($request){
                    $query->where('process_number',$request->process_number);
            });
        }
        
        if(isset($request->is_finished)){
            $icRecords = $icRecords->whereHas('weightdata',function($query) use ($request){
                    $query->where('is_finished',$request->is_finished);
            });
        }

        if(isset($request->is_success)){
            $icRecords = $icRecords->whereHas('weightdata',function($query) use ($request){
                    $query->where('is_success',$request->is_success);
            });
        }


        $icRecords = $icRecords->with(['weightdata','weightAbnormal','flowExtemal']);

        if(request()->has('page')){
            $pageSize = 10;
            if(request()->has('pageSize')){
                $pageSize = request()->pageSize;
            }
            $icRecords = $icRecords->orderBy('system_datetime','desc')->paginate();
        }
        else{
            $icRecords = $icRecords->get();
        }

    	return formatJsonResponse($icRecords);
    }

    public function updatePurchaseWeight($id,Request $request){

    	$flowExtemal = FlowExtemal::where('order_systemnumber',$id)->first();

    	$flowExtemal->purchase_weight = $request->purchase_weight;

    	$flowExtemal->save();

    	return formatJsonResponse();
    }

    public function updateDischargeWeight($id,Request $request){

    	$flowExtemal = FlowExtemal::where('order_systemnumber',$id)->first();

    	$flowExtemal->discharge_weight = $request->discharge_weight;

    	$flowExtemal->save();

    	return formatJsonResponse();
    }

    public function updatePickWeight($id,Request $request){

    	$flowExtemal = FlowExtemal::where('order_systemnumber',$id)->first();

    	$flowExtemal->pick_weight = $request->pick_weight;

    	$flowExtemal->save();

    	return formatJsonResponse();
    }

    public function updateOutWeight($id,Request $request){

    	$flowExtemal = FlowExtemal::where('order_systemnumber',$id)->first();

    	$flowExtemal->out_weight = $request->out_weight;

    	$flowExtemal->save();

    	return formatJsonResponse();
    }

    public function updateIsReturn($id,Request $request){

    	$flowExtemal = FlowExtemal::where('order_systemnumber',$id)->first();

    	$flowExtemal->is_return = $request->is_return;

    	$flowExtemal->save();

    	return formatJsonResponse();
    }

    public function updateIsGo($id,Request $request){

    	$flowExtemal = FlowExtemal::where('order_systemnumber',$id)->first();

    	$flowExtemal->is_go = $request->is_go;

    	$flowExtemal->save();

    	return formatJsonResponse();
    }

    public function updateRemark($id,Request $request){

    	$flowExtemal = FlowExtemal::where('order_systemnumber',$id)->first();

    	$flowExtemal->remarks = $request->remarks;

    	$flowExtemal->save();

    	return formatJsonResponse();
    }

    public function isReturn(){
    	return formatJsonResponse(itemOption(FlowExtemal::$IS_RETURN));
    }

    public function isGo(){
    	return formatJsonResponse(itemOption(FlowExtemal::$IS_GO));
    }


    public function export(Request $request){
        return Excel::download(new FlowExtemalExport($request->all()), '流程表（外部车辆）'. time() .'.xlsx');
    }
}
